﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;

namespace WebSiteAlex
{
    public partial class ControlExpenseReport : System.Web.UI.UserControl
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            //this.ExpRepId = "8416dd14-9351-4efd-94db-28710582d4fe";
            this.DataBind();
        }

        public override void DataBind()
        {
            if (this.ExpRepId != null)
            {
                ExpenseReport e = ExpenseReport.Get(this.ExpRepId.Value);
                this.tb_Comments.Text = e.Comments;
                this.tb_Name.Text = e.Name;
                this.tb_UserEmail.Text = e.UserEmail;
                this.expenseReportLineControl.ExpenseReportId = this.ExpRepId.Value;
            }
            base.DataBind();
        }

        public Guid? ExpRepId
        {
            get
            {
                if (ViewState["Id"] != null)
                    return (Guid)ViewState["Id"];
                else
                    return null;
            }
            set { ViewState["Id"] = value; }
        }

        public void Save()
        {
            String name = this.tb_Name.Text;
            String comments = this.tb_Comments.Text;

            ExpenseReport expenseReport = new ExpenseReport();
            expenseReport.Name = name;
            expenseReport.Comments = comments;
            expenseReport.State = "Waiting";
            //user é o que estiver autenticado, ver nos cookies provávelmente
            /*Mas atenção que há coisas já feitas, forms authentication etc*/

            //temporariamente o user é introduzido numa textbox.
            expenseReport.UserEmail = this.tb_UserEmail.Text;
            if (this.ExpRepId == null)
                expenseReport.Id = Guid.NewGuid();
            else
                expenseReport.Id = this.ExpRepId.Value;
            expenseReport.CreationDate = DateTime.Now;

            //submit para a DB
            expenseReport.Save();
            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
            //    dc.ExpenseReports.InsertOnSubmit(expenseReport);
            //    dc.SubmitChanges();
            //}
        }
    }
}